package com.ttn.auth.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttn.auth.service.SysMenuService;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.auth.SysMenu;
import com.ttn.common.core.entity.auth.query.SysMenuPageQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * (sys_menu)表控制层
 *
 * @author xxxxx
 */
@RestController
@Slf4j
@RequestMapping("/sys-menu")
public class SysMenuController {
    /**
     * 服务对象
     */
    @Resource
    private SysMenuService sysMenuService;

    /**
     * 保存菜单
     *
     * @param request
     * @param sysMenu
     * @return
     */
    @PostMapping("/save/sys-menu")
    public Result saveSysMenu(HttpServletRequest request, @RequestBody SysMenu sysMenu) {
        log.info("SysMenuController saveSysMenu ,参数:{}", sysMenu);
        sysMenuService.save(sysMenu);
        return Result.success();
    }

    /**
     * 修改菜单
     *
     * @param request
     * @param sysMenu
     * @return
     */

    @PutMapping("/modify/sys-menu")
    public Result modifySysMenu(HttpServletRequest request, @RequestBody SysMenu sysMenu) {
        log.info("SysMenuController modifySysMenu ,参数:{}", sysMenu);
        sysMenuService.updateById(sysMenu);
        return Result.success();
    }

    /**
     * 分页查询菜单
     *
     * @param request
     * @param sysMenuPageQuery
     * @return
     */
    @PostMapping("/query/sys-menu-page")
    public Result querySysMenuPage(HttpServletRequest request, @RequestBody SysMenuPageQuery sysMenuPageQuery) {
        log.info("SysMenuController querySysMenuPage ,参数:{}", sysMenuPageQuery);
        IPage<SysMenu> sysMenuIPage = new Page<>(sysMenuPageQuery.getPageNum(), sysMenuPageQuery.getPageSize());
        LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysMenuLambdaQueryWrapper.eq(StrUtil.isNotBlank(sysMenuPageQuery.getTitle()), SysMenu::getTitle,
                sysMenuPageQuery.getTitle());
        sysMenuLambdaQueryWrapper.eq(Objects.nonNull(sysMenuPageQuery.getVisible()), SysMenu::getVisible,
                sysMenuPageQuery.getVisible());
        sysMenuLambdaQueryWrapper.eq(Objects.nonNull(sysMenuPageQuery.getStatus()), SysMenu::getStatus,
                sysMenuPageQuery.getStatus());
        sysMenuIPage = sysMenuService.page(sysMenuIPage, sysMenuLambdaQueryWrapper);
        TreeNodeConfig config = new TreeNodeConfig();
        config.setParentIdKey("parentId");
//        config.setDeep(20);//最大递归深度  默认无限制
        List<Tree<Integer>> treeNodes = TreeUtil.build(sysMenuIPage.getRecords(), 0, config, (object, tree) -> {
            tree.setId(object.getId());//必填属性
            tree.setParentId(object.getParentId());//必填属性
            tree.setName(object.getName());
            tree.putExtra("title", object.getTitle());
            tree.putExtra("path", object.getPath());
            tree.putExtra("component", object.getComponent());
            tree.putExtra("resources", object.getResources());
            tree.putExtra("redirect", object.getRedirect());
            tree.putExtra("icon", object.getRedirect());
            tree.putExtra("parm", object.getRedirect());
            tree.putExtra("sort", object.getRedirect());
            tree.putExtra("visible", object.getRedirect());
            tree.putExtra("status", object.getRedirect());
        });
        return Result.success(treeNodes);
    }


}
